from scoreboard import Scoreboard
from button import Button
from game_stats import GameStats
from pygame.sprite import Group
from settings import Settings
from ship import Ship
import game_functions as gf
import pygame


def run_game():
    # 初始化 pygame、设置和屏幕对象
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))  # 创建一个指定像素的窗口
    pygame.display.set_caption("外星人入侵")  # 窗口标题
    # 创建一个用于存储游戏统计信息的实例
    stats = GameStats(ai_settings)
    # 创建开始按钮
    play_button = Button(ai_settings, screen, 'Play')
    # 创建记分牌
    sb = Scoreboard(ai_settings, screen, stats)
    # 创建一艘飞船，一个外星人编组和子弹编组
    ship = Ship(ai_settings, screen)
    aliens = Group()
    bullets = Group()
    # 开始游戏的主循环
    while True:
        # 获取游戏运行时间毫秒数
        game_runtime = pygame.time.get_ticks()
        # 监控键盘和鼠标事件
        gf.check_events(ai_settings, screen, stats, ship, play_button, aliens, bullets, game_runtime, sb)
        if stats.game_active:
            # 飞船移动
            ship.update()
            # 发射子弹
            gf.open_fire(ai_settings, screen, ship, bullets, game_runtime, aliens, sb, stats)
            # 创建外星人部队
            gf.create_fleet(ai_settings, screen, aliens, game_runtime)
            # 检测外星人和飞船的碰撞
            gf.ship_hit(ai_settings, stats, screen, ship, aliens, bullets, game_runtime)
        # 每次循环时都重绘屏幕
        gf.update_screen(ai_settings, screen, stats, ship, aliens, bullets, play_button, sb)


run_game()
